package com.wip.reportingservice.daos.impls;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.wip.reportingservice.daos.entities.PageClientTimeTable;
import com.wip.reportingservice.daos.interfaces.IPageClientTimeDao;

@Repository
@Transactional
public class PageClientTimeDaoImpl implements IPageClientTimeDao {

	private static final Logger logger = LoggerFactory
			.getLogger(PageClientTimeDaoImpl.class);

	@Autowired
	private SessionFactory sessionFactory;

	public void save(PageClientTimeTable page) {
		logger.info("PageClientTimeDaoImpl.save");

		sessionFactory.getCurrentSession().save(page);

	}

	public void update(PageClientTimeTable page) {
		logger.info("PageClientTimeDaoImpl.update");

		sessionFactory.getCurrentSession().update(page);

	}

	public void delete(PageClientTimeTable page) {
		logger.info("PageClientTimeDaoImpl.delete");
		sessionFactory.getCurrentSession().delete(page);

	}

	public PageClientTimeTable getById(int id) {
		logger.info("PageClientTimeDaoImpl.getById");

		final Criteria crit = sessionFactory.getCurrentSession()
				.createCriteria(PageClientTimeTable.class);
		crit.add(Restrictions.eq("id",id));
		return (PageClientTimeTable) crit.list().get(0);

	}

	@Override
	public PageClientTimeTable getByName(String name) {
		logger.info("PageClientTimeDaoImpl.getByName");

		final Criteria crit = sessionFactory.getCurrentSession()
				.createCriteria(PageClientTimeTable.class);

		crit.add(Restrictions.ilike("page_name", name));

		return (PageClientTimeTable) crit.list().get(0);
	}

	@Override
	//All Clients, All Logs, Specific Page
	public List<PageClientTimeTable> getRecord(int pageId) {
		logger.info("PageClientTimeDaoImpl.getRecord");

		final Criteria crit = sessionFactory.getCurrentSession()
				.createCriteria(PageClientTimeTable.class);

		crit.add(Restrictions.eq("pageId", pageId));

		return crit.list();
	}

	@Override
	//Specific page, Specific client, All Logs
	public List<PageClientTimeTable> getRecord(int pageId, int clientId) {
		logger.info("PageClientTimeDaoImpl.getRecord");

		final Criteria crit = sessionFactory.getCurrentSession()
				.createCriteria(PageClientTimeTable.class);

		Criterion pageIdCriterion = Restrictions.eq("pageId", pageId);
		Criterion clientIdCriterion = Restrictions.eq("clientId", clientId);

		crit.add(Restrictions.and(pageIdCriterion, clientIdCriterion));

		return crit.list();
	}

	@Override
	//specific page, specific client, specific log
	public List<PageClientTimeTable> getRecord(int pageId, int clientId,
			int logId) {
		logger.info("PageClientTimeDaoImpl.getRecord");

		final Criteria crit = sessionFactory.getCurrentSession()
				.createCriteria(PageClientTimeTable.class);

		Criterion pageIdCriterion = Restrictions.eq("pageId", pageId);
		Criterion clientIdCriterion = Restrictions.eq("clientId", clientId);
		Criterion logIdCriterion = Restrictions.eq("logId", logId);

		crit.add(Restrictions.and(
				Restrictions.and(pageIdCriterion, clientIdCriterion),
				logIdCriterion));

		return crit.list();
	}

	@Override
	public List<PageClientTimeTable> getRecord(List<Integer> pages,
			List<Integer> clients, List<Integer> logs) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PageClientTimeTable.class);

		Disjunction clientsDisjunction = Restrictions.disjunction();

		for(int clientNum:clients)
		{
		    int clientId = 1;//clientDao.getByName(clientName).getClientId();
		   
		    //joing all the client ids by or
		    clientsDisjunction.add(Restrictions.eq("clientId", clientNum));


		}

		Disjunction logsDisjunction = Restrictions.disjunction();

		for(int logNum:logs)
		{
		    int logId = 1;//logDao.getByName(logName).getLogId();
		   
		    //joing all the client ids by or
		    logsDisjunction.add(Restrictions.eq("logRunDate", logNum));


		}


		Disjunction pagesDisjunction = Restrictions.disjunction();

		for(int pageNum:pages)
		{
		    int pageId = 1;//pageDao.getByName(pageName).getPageId();
		   
		    //joining all the client ids by or
		    pagesDisjunction.add(Restrictions.eq("pageName", pageNum));


		}

		//and all the or conditions
		Criterion completeCondition =
		    Restrictions.conjunction().add(clientsDisjunction)
		                              .add(logsDisjunction)
		                              .add(pagesDisjunction);
		                             
		criteria.add(completeCondition);
		
		return criteria.list();
	}
	
	@Override
	public  List<PageClientTimeTable> getAll() {
		return null;
	}
	

}
